//
//  ViewController.swift
//  testSwift
//
//  Created by taoge on 2018/2/24.
//  Copyright © 2018年 FT. All rights reserved.
//

import UIKit
import Alamofire
import Kingfisher
import SwiftyJSON
import HandyJSON
struct FTModel:  HandyJSON{
    var cover_image_url = ""
    var title = ""
}
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,FTViewDelegate {
   lazy  var _tableView = UITableView()
   lazy    var dataArr = [FTModel]()
//  lazy    var dataArr = [HomeNewsTitle]()
   lazy var testView = FTView()
    var type = clickType(rawValue: 0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        creat()
        loadData()

//       loadOtherData()
    }
    /*
    func loadOtherData(){
        NetworlTool.loadNewTitleData { (response) in
            self.dataArr = response
            self._tableView.reloadData()
            
        }
    }
 */
    func creat(){
        _tableView = UITableView(frame: CGRect(x: 0, y: 30, width: self.view.bounds.size.width, height: self.view.bounds.size.height-30), style: UITableViewStyle.plain)
        _tableView.delegate = self
        _tableView.dataSource = self
        self.view.addSubview(_tableView)
        let nib = UINib.init(nibName: "FTTableViewCell", bundle: nil)
        _tableView.register(nib, forCellReuseIdentifier: "FTTableViewCell")
        _tableView.tableFooterView = UIView()
       
        testView = FTView.newInstance()!
        testView.frame = CGRect.init(x: Width/2-50, y: 0, width: 100, height: 100)
        testView.layer.masksToBounds = true
        testView.layer.cornerRadius = 50
        testView.backgroundColor = UIColor.green
        testView.delegate = self
        self.view.addSubview(testView)
        
    }
    func clickButton(sender: UIButton) {
         print("111111")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 250
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        let cell = tableView.dequeueReusableCell(withIdentifier: "FTTableViewCell", for: indexPath) as! FTTableViewCell
       
        if self.dataArr.count > 0 {
            let model = self.dataArr[indexPath.row]
            cell.titleLable.text = model.title
            let url = URL(string: model.cover_image_url)
            cell.MymageView.kf.setImage(with: url)
        }
       
       return cell
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
       
        print("==============\(indexPath.row)")
        switch indexPath.row {
        
        case 0:
        
            let vc = SecongViewController()
            present(vc, animated: true, completion: nil)
            
        case 1:
            let vc = ThreeViewController()
            present(vc, animated: true, completion: nil)
       
        default: print("==============\(indexPath.row)")
            
        }
        
        
    }
    
    func loadData(){
        Alamofire.request("http://api.liwushuo.com/v2/channels/104/items?ad=2&gender=2&generation=2&limit=20&offset=0",
         method: .get).responseJSON { (response) in
          print("\(response)")
            guard response.result.isSuccess else {return}
            
            if let jsongResult = response.result.value{
                let jsonDic = JSON(jsongResult)
                
/*
                let data = jsonDic["data"]["items"].array
                for dataDic in data!{
                    let  model = FTModel()
                    //  ?? 这个符号，我怕有初学者忘记了的提醒一下，A ?? B  这是一个 NIL合并运算符，它的作用是如果 A 不是NIL 就返回前面可选类型参数 A 的确定值， 如果 A 是NIL 就返回后面 B 的值！A和B之间类型的注意点我就不说了，忘记了去看书，，哈哈哈
                    model.cover_image_url = dataDic["cover_image_url"].string ?? ""
                    model.title = dataDic["title"].string ?? ""
                    self.dataArr.append(model)
                 }
//                self.view.makeToast("\(String(describing: data))", duration: 20, position: CSToastPositionTop)
                self.view.makeToast("-无情涛哥-", duration: 1, position: CSToastPositionCenter)
*/
                if let dic = jsonDic["data"].dictionary{
                    
                    
                    if  let  resultDic = dic["items"]!.arrayObject{

                        let model = resultDic.flatMap({FTModel.deserialize(from: $0 as? Dictionary)})
                        self.dataArr = model
                    }
                    
                }
                
                 self._tableView.reloadData()
            }
        }
   }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

